package org.skipper.samples.spring.transaction.manager;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * Description: 自定义事务管理器
 * todo 完善场景
 *
 * @author shoujian
 * Date: 2022-02-11
 * Time: 9:43 AM
 */
@Component
public class SelfTransactionManager  {

    private TransactionStatus transactionStatus;

    //获取事务源
    @Resource
    private DataSourceTransactionManager dataSourceTransactionManager;

    /**
     * 手动开启事务
     * @return
     */
    public TransactionStatus begin(){
        transactionStatus = dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
        return transactionStatus;
    }

    /**
     * 提交事务
     * @param transactionStatus
     */
    public void commit(TransactionStatus transactionStatus){
        dataSourceTransactionManager.commit(transactionStatus);
    }

    /**
     * 回滚事务
     */
    public void rollBack(){
        dataSourceTransactionManager.rollback(transactionStatus);
    }
}
